package qc.module.ehs.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.EhsUnitStaticinfoTypeEnum;
import qc.module.ehs.dto.unitstaticinfo.EhsUnitStaticInfoDto;
import qc.module.ehs.entity.EhsUnitStaticinfo;

import java.util.List;

/**
 * EhsUnitStaticInfoMapper
 *
 * @author QuCheng Tech
 * @create 2024/8/14
 */
@Mapper
public interface EhsUnitStaticInfoMapper {

    EhsUnitStaticInfoMapper MAPPER = Mappers.getMapper(EhsUnitStaticInfoMapper.class);

    @Mapping(target = "infotype", source = "infotype.index")
    EhsUnitStaticInfoDto toDto(EhsUnitStaticinfo en);

    @Mapping(target = "infotype", ignore = true)
    EhsUnitStaticinfo toEntity(EhsUnitStaticInfoDto dto);

    default EhsUnitStaticinfo dtoToEntity(EhsUnitStaticInfoDto dto) {
        EhsUnitStaticinfo en = EhsUnitStaticInfoMapper.MAPPER.toEntity(dto);

        en.setInfotype(getEhsUnitStaticinfoTypeEnum(dto.getInfotype()));

        return en;
    }

    static EhsUnitStaticinfoTypeEnum getEhsUnitStaticinfoTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (EhsUnitStaticinfoTypeEnum item : EhsUnitStaticinfoTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }

        //单元静态信息类型默认为1-文字
        return EhsUnitStaticinfoTypeEnum.CHARACTERS;
    }
    
    List<EhsUnitStaticInfoDto> toDtoList(List<EhsUnitStaticinfo> ens);
}
